package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.AreaCodeDTO;
import com.htgd.gardenexpo.dto.GeConSocialCritiqueDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConLonLat;
import com.htgd.gardenexpo.entity.GeConSocialContent;
import com.htgd.gardenexpo.page.CritiqueQuery;
import com.htgd.gardenexpo.page.SocialContentQuery;
import com.htgd.gardenexpo.service.IGeConSocialContentService;
import com.htgd.gardenexpo.utils.IpUtils;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GeConCritiqueVO;
import com.htgd.gardenexpo.vo.GeConSocialContentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 社交内容 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-08
 */
@Slf4j
@RestController
@RequestMapping("/api/socialContent")
@Api(tags = "社交内容")
public class GeConSocialContentController {

    @Autowired
    private IGeConSocialContentService geConSocialContentService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult add(@RequestBody GeConSocialContent geConSocialContent, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("社交内容-添加：{},{}",geConSocialContent,openId);
        geConSocialContent.setIp(IpUtils.getUserIP(request));
        return geConSocialContentService.add(geConSocialContent,openId);
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConSocialContentVO>> list(SocialContentQuery socialContentQuery, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        PageInfo<GeConSocialContentVO> list = geConSocialContentService.pageList(socialContentQuery,openId);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }


    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete(String id) {
        log.info("社交内容-删除：{}",id);
        geConSocialContentService.removeById(id);
        return CommonResult.success("删除成功");
    }

    @PostMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult review(@RequestBody GeConSocialContent geConSocialContent) {
        geConSocialContentService.updateById(geConSocialContent);
        return CommonResult.success("修改成功");
    }


    @PostMapping(value = "/getAreaCode")
    @ApiOperation("小程序根据经纬度获取地址")
    public CommonResult<JSONObject> getAreaCode(@RequestBody AreaCodeDTO areaCode, HttpServletRequest request) {
        CommonResult<JSONObject> result1 = new CommonResult<>();
        log.info("根据经纬度获取地区code：经度===>【{}】,纬度===>【{}】",areaCode.getLatitude(),areaCode.getLongitude());
        Map<String, Object> map = new HashMap<String, Object>();
        List<GeConLonLat> geConLonLatList = geConSocialContentService.selectGeConLonLat(areaCode.getLongitude(),areaCode.getLatitude());
        if(!geConLonLatList.isEmpty()){
            map.put("pro",geConLonLatList.get(0).getPro());
            map.put("city",geConLonLatList.get(0).getCity());
            map.put("district",geConLonLatList.get(0).getDistrict());
            result1.setCode(200);
            result1.setMessage("查询成功");
            JSONObject jsonObject = new JSONObject(map);
            result1.setData(jsonObject);
            return result1;
        }
        try {
            map = geConSocialContentService.getAreaCode(areaCode.getLongitude(),areaCode.getLatitude(),IpUtils.getUserIP(request));
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error("查询失败");
        }
        result1.setCode(200);
        result1.setMessage("查询成功");
        JSONObject jsonObject = new JSONObject(map);
        result1.setData(jsonObject);
        return result1;
    }

    @PostMapping(value = "/likes")
    @ApiOperation("点赞及取消点赞")
    public CommonResult likes(@RequestBody String contentId, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        return geConSocialContentService.likes(contentId,openId);
    }

    @PostMapping(value = "/commentReply")
    @ApiOperation("评论及回复")
    public CommonResult commentReply(@RequestBody GeConSocialCritiqueDTO geConSocialCritique, HttpServletRequest request) {
        log.info("社交内容-评论及回复：{},{}",geConSocialCritique,request);
        return geConSocialContentService.commentReply(geConSocialCritique,request);
    }

    @PostMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeConSocialContentVO> details(@RequestBody String contentId, HttpServletRequest request){
        CommonResult<GeConSocialContentVO> result = new CommonResult<>();
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        try{
            GeConSocialContentVO geConSocialContentVO = geConSocialContentService.getGeConSocialContentById(contentId,openId);
            result.setData(geConSocialContentVO);
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getPageCritiqueList")
    @ApiOperation("获取社交内容评论列表")
    public CommonResult<List<GeConCritiqueVO>> getPageCritiqueList(CritiqueQuery critiqueQuery, HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        PageInfo<GeConCritiqueVO> list = geConSocialContentService.getPageCritiqueList(critiqueQuery,openId);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

    @GetMapping(value = "/getPageCritiqueReplyList")
    @ApiOperation("获取社交内容评论回复列表")
    public CommonResult<List<GeConCritiqueVO>> getPageCritiqueReplyList(CritiqueQuery critiqueQuery, HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        PageInfo<GeConCritiqueVO> list = geConSocialContentService.getPageCritiqueReplyList(critiqueQuery,openId);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

}
